###############################################################################
#
# file: Player.py
# author: Victor Bos
# 
# Copyright 2007--2011 Victor Bos
#
# This file is part of GREATOR.
#
#    GREATOR is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    GREATOR is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GREATOR.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
import unittest
import PasswordDialog

class Player:

    def __init__(self, name):
        """
        Creates a player with given name.

        The Player's password to None.
        """
        self.name = name
        self.set_password(None)
        self.attrs = {}

    def set_password(self, pw):
        """
        Sets the Player's password to PasswordDialog.hash_password(pw).
        """
        self.password = PasswordDialog.hash_password(pw)

    def check_password(self, pw):
        """
        Checks if PasswordDialog.hash_password(pw) equals the
        Player's password.

        Returns True if PasswordDialog.hash_password(pw) is equal to
        the Player's password, False otherwise.
        """
        return self.password == PasswordDialog.hash_password(pw)

    def has_password(self):
        return self.password is not None

    def get_save_str(self):
        result = []
        result.append("begin Player")
        result.append("  name: %s" % self.name)
        pwstr = "None"
        if self.password is not None:
            pwstr = self.password.encode("hex_codec")
        result.append("  password: %s" % pwstr)
        for a in self.attrs:
            result.append("  %s: %s" % (a, self.attrs[a]))
        result.append("end Player")
        return "\n".join(result)

    def set_password_from_save_str(self, svstr):
        self.password = svstr.decode("hex_codec")
        
class TestPlayer(unittest.TestCase):

    def setUp(self):
        self.p1 = Player("Wim")
        self.p2 = Player("Wilma")
        self.p3 = Player("Wilma")
        
    def test__init__(self):
        self.assertNotEqual(self.p1.name, self.p2.name)
        self.assertNotEqual(self.p1.name, self.p3.name)
        self.assertEqual(self.p2.name, self.p3.name)
        self.assertEqual(self.p1.password, self.p2.password)
        self.assertEqual(self.p2.password, self.p3.password)
        self.assertNotEqual(self.p1.password, self.p1.name)
        self.assertNotEqual(self.p2.password, self.p2.name)
        self.assertNotEqual(self.p3.password, self.p3.name)
        
    def test_set_password(self):
        self.assert_(self.p1.password is self.p3.password)
        self.p1.set_password(self.p1.name)
        self.p2.set_password(self.p1.name)
        self.assertEqual(self.p1.password, self.p2.password)

    def test_check_password(self):
        self.assert_(self.p1.check_password(None))
        self.assert_(not self.p2.check_password(self.p1.name))
        self.p3.set_password(self.p3.name)
        self.assert_(self.p3.check_password(self.p3.name))

    def test_has_password(self):
        self.assert_(not self.p1.has_password())
        self.p2.set_password("new password")
        self.assert_(self.p2.has_password())
        self.p1.set_password(self.p1.name)
        self.assert_(self.p1.has_password())
        
###############################################################################
if __name__ == '__main__':
    unittest.main()
